//
//  SXStatusListViewModel.swift
//  sina
//
//  Created by shengxinwei on 16/5/25.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit

class SXStatusListViewModel: NSObject {
    

    lazy var statusViewModel: [SXStatusViewModel] = [SXStatusViewModel]()

    func loadData(ispull: Bool,finished: (isSuccess: Bool) -> ()) {
        
        var since_id: Int64 = 0
        var max_id: Int64 = 0

        if ispull {
            max_id = statusViewModel.last?.status?.id ?? 0
        }else {
            since_id = statusViewModel.first?.status?.id ?? 0
        }
        
        SXNetworkTools.sharedTools.loadHomePageData(since_id,max_id: max_id) { (responseObject, error) in
            if error != nil {
                print(error)
                finished(isSuccess: false)
                return
            }
            
            guard let dict = responseObject as? [String: AnyObject] else {
                finished(isSuccess: false)
                return
            }
            
            guard let arr = dict["statuses"] as? [[String: AnyObject]] else{
                finished(isSuccess: false)
                return
            }
            //实例化数组
            var tempArr = [SXStatusViewModel]();
            
            for item in arr {
                let s = SXStatus(dict: item)
                let viewModel = SXStatusViewModel(status: s)
                tempArr.append(viewModel)
            }
            
            if ispull  {
                self.statusViewModel = self.statusViewModel + tempArr
            }else {
                //下拉刷新 , 新数据 + 老数据
                self.statusViewModel = tempArr + self.statusViewModel
            }
            
            finished(isSuccess: true)
            
        }
    }

}
